package work.utils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileUtils {
  /*  public static void main(String[] args) throws IOException {

        FileInputStream input = new FileInputStream(new File("C:\\Users\\dinglei\\Desktop\\三化账号.zip"));
        ZipInputStream stream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));

        String dirPath;
        Map<String, byte[]> files = new HashMap<>();
        ZipEntry entry;
        while ((entry = stream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                dirPath = entry.getName();


                //清空所有文件
                files.clear();

            } else {

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                int len;
                byte[] buff = new byte[1024];
                while ((len = stream.read(buff)) != -1) {
                    byteArrayOutputStream.write(buff, 0, len);
                }

                //放入所有文件
                files.put(entry.getName(), byteArrayOutputStream.toByteArray());

            }
        }


*//*
        final MyFile root = new MyFile();

        root.setName("");
        root.setAbsName("");

        //组装树结构
        ZipEntry entry;
        while ((entry = stream.getNextEntry()) != null) {

            String name = entry.getName();
            String[] split = name.split("/");
            MyFile f = root;

            for (int i = 0; i < split.length - 1; i++) {
                MyFile sub = f.findSub(split[i]);
                if (sub == null) {
                    sub = new MyFile();
                    sub.setAbsName(f.absName + split[i] + "/");
                    sub.dir = true;
                    sub.setName(split[i]);
                    sub.parent = f;
                    f.addSub(sub);
                }
                f = sub;
            }

            MyFile sub = f.findSub(split[split.length - 1]);
            if (sub == null) {
                sub = new MyFile();
                if (entry.isDirectory()) {
                    sub.dir = true;
                    sub.setAbsName(f.absName + split[split.length - 1] + "/");
                } else {
                    sub.setAbsName(f.absName + split[split.length - 1]);
                }
                sub.setName(split[split.length - 1]);
                sub.parent = f;
                f.addSub(sub);
            }
            //System.out.println(name + "       " + split[split.length - 1]);
            stream.closeEntry();
        }

        stream.close();

        //对文件进行一次梳理
        handle(root);*//*

        //如果是文件就把文件读取到一个map中,知道下次读取到文件夹时再进行处理

    }
*/
/*

    private static void handle(MyFile myFile) {

        Set<MyFile> subs = myFile.getSubs();
        if (subs != null) {
            boolean flag = true;
            for (MyFile sub : subs) {
                if (sub.isDir()) {
                    handle(sub);
                    flag = false;
                }
            }
            myFile.setAllFile(flag);
        }

    }

    @Getter
    @Setter
    public static class MyFile {
        private boolean dir;
        private String name;
        private String absName;
        private Set<MyFile> subs;
        private MyFile parent;
        private boolean allFile;

        //真实父路径
        private String realParent;

        //格式化后的名称
        private String formatName;

        //由格式化后的路径中提取出的用户名称
        private String user;

        //有格式化后的路径中提取出的身份证号
        private String idcard;

        public void setAllFile(boolean allFile) {
            this.allFile = allFile;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAbsName(String absName) {
            this.absName = absName;
        }

        public void addSub(MyFile file) {
            if (subs == null) {
                dir = true;
                subs = new HashSet<>();
            }
            subs.add(file);
        }

        public MyFile findSub(String name) {
            if (subs != null) {
                for (MyFile sub : subs) {
                    if (sub.name.equals(name)) {
                        return sub;
                    }
                }
            }
            return null;
        }

    }
*/


}
